package jprint.jfx.webapp;

import java.util.Map;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;
import jprint.base.AppContext;
import jprint.jfx.AppContextImpl;
import jprint.jfx.AppStarting;

/**
 * @author zwz
 * Created on 2021-01-21
 */
public class WebViewApp extends Application {

    private static AppStarting appStarting;
    private WebViewController controller;

    @Override public void init() throws Exception {
        super.init();
        updateProgress("应用正在初始化...", 25);
        AppContext appContext = AppContextImpl.getInstance();
        setPrintParams(appContext);
        System.out.println(appContext);
    }

    @Override public void start(Stage stage) throws Exception {
        updateProgress("应用正在启动...", 66);
        stage.setTitle("标签打印");
        stage.setWidth(1024);
        stage.setHeight(768);
        stage.setOnCloseRequest(event -> {
            // event.consume(); // 消耗掉此事件可以阻止窗口关闭
            System.out.println("App closing!");
            stage.close();
            try {
                stop();
            } catch (Exception e) {
                e.printStackTrace();
            }
            AppContextImpl.getInstance().exit();
            Platform.exit();
            System.exit(0);
        });
        Platform.runLater(() -> {
            try {
                controller = new WebViewController();
                controller.loadUrl("http://localhost:2020/index.html");
                stage.setScene(controller.getScene());
            } catch (Exception e) {
                e.printStackTrace();
            }
            updateProgress("应用启动完成.", 100);
            if (appStarting != null) appStarting.startComplete();
        });
        stage.show();
    }

    private void setPrintParams(AppContext appContext) {
        Parameters parameters = getParameters();
        Map<String, String> named = parameters.getNamed();
        if (named == null || named.isEmpty()) return;

        updateProgress("设置打印参数...", 30);
        System.out.println("Set print params:");
        System.out.println(named);
        if (named.containsKey("printerDPI")) {
            appContext.setPrinterDPI(Integer.parseInt(named.get("printerDPI")));
        }
        if (named.containsKey("paperWidth")) {
            appContext.setPaperWidth(Integer.parseInt(named.get("paperWidth")));
        }
        if (named.containsKey("paperHeight")) {
            appContext.setPaperHeight(Integer.parseInt(named.get("paperHeight")));
        }
        if (named.containsKey("scale")) {
            appContext.setScale(Double.parseDouble(named.get("scale")));
        }
        if (named.containsKey("offset")) {
            String[] offset = named.get("offset").split(",");
            appContext.setOffset(Integer.parseInt(offset[0]), Integer.parseInt(offset[1]));
        }
    }

    private void updateProgress(String text, int progress) {
        if (appStarting == null) return;
        appStarting.updateProgress(text, progress);
    }

    public static void setAppStarting(AppStarting appStarting) {
        WebViewApp.appStarting = appStarting;
    }
}

